package deployr.core;

import static java.text.MessageFormat.*;

import java.io.*;

import deployr.client.api.*;
import deployr.client.http.*;

import sun.reflect.generics.reflectiveObjects.*;

public class Deployr implements DeployrApi {

	private final String deployrUrl;

	private final UpdateApi update;

	private final ProjectApi project;

	private final UploadApi upload;

	public Deployr(String deployrUrl) {
		this.deployrUrl = deployrUrl;
		this.update = new UpdateHttpClient();
		this.project = new ProjectHttpClient();
		this.upload = new UploadHttpClient();
	}

	@Override
	public void check(String projectKey) {
	}

	@Override
	public String checksum(String projectKey) {
		return project.readChecksum(deployrUrl, projectKey);
	}

	@Override
	public void cloning(String projectKey, String updateKey, String what) {
		update.createEvent(deployrUrl, updateKey, format("cloning {0}", what));
	}

	@Override
	public void closeUpdate(String projectKey, String updateKey) {
		update.createEvent(deployrUrl, updateKey, "close update");
	}

	@Override
	public void closeUpgrade(String app1, String app1update) {
	}

	@Override
	public void deployingNewVersion(String projectKey, String updateKey) {
		update.createEvent(deployrUrl, updateKey, "deploying new version");
		project.war(deployrUrl, projectKey);
		project.war(deployrUrl, projectKey, updateKey);
	}

	@Override
	public String index(String projectKey) {
		throw new NotImplementedException();
	}

	@Override
	public void initiatingUpdate(String projectKey, String updateKey) {
		update.createEvent(deployrUrl, updateKey, "initiating update");
	}

	@Override
	public void initiatingUpgrade(String app1, String app1update) {
		throw new NotImplementedException();
	}

	@Override
	public String startUpdate(String projectKey) {
		return project.createUpdate(deployrUrl, projectKey);
	}

	@Override
	public String startUpgrade(String app1, String awsAccessKey, String awsSecretKey) {
		throw new NotImplementedException();
	}

	@Override
	public void success(String projectKey, String updateKey) {
		update.createEvent(deployrUrl, updateKey, "success");
		project.createEvent(deployrUrl, projectKey, "update");
	}

	@Override
	public void updateChecksum(String projectKey, String updateKey, String checksum) {
		update.updatechecksum(deployrUrl, updateKey, checksum);
	}

	@Override
	public void updateComplete(String projectKey, String updateKey) {
		update.createEvent(deployrUrl, updateKey, "update complete");
	}

	@Override
	public void updateDiff(String projectKey, String updateKey, String diff) {
		update.updateDiff(deployrUrl, updateKey, diff);
	}

	@Override
	public void updateProjectChecksum(String projectKey, String checksum) {
		project.updateChecksum(deployrUrl, projectKey, checksum);
	}

	@Override
	public void upload(String bucketName, String updateKey, String awsAccessKey, String awsSecretKey, File file) {
		upload.upload(deployrUrl, updateKey, file);
		String fileName = file.getName();
		update.createDeliverable(deployrUrl, updateKey, fileName, format("http://{0}/updates/{1}/files/{2}", deployrUrl, updateKey, fileName), Md5.md5(file));
	}

	@Override
	public void uploadingPatchDescription(String projectKey, String updateKey) {
		update.createEvent(deployrUrl, updateKey, "uploading patch description");
	}

	@Override
	public void failure(String projectKey, String updateKey) {
		update.createEvent(deployrUrl, updateKey, "failure");
		project.createEvent(deployrUrl, projectKey, "failure");
	}

	@Override
	public void webapp(String projectKey) {
		project.webapp(deployrUrl, projectKey);
	}

	@Override
	public void webapp(String projectKey, String updateKey) {
		project.webapp(deployrUrl, projectKey, updateKey);
	}

	@Override
	public void war(String projectKey) {
		project.war(deployrUrl, projectKey);
	}

	@Override
	public void war(String projectKey, String updateKey) {
		project.war(deployrUrl, projectKey, updateKey);
	}

	@Override
	public void createProject(String projectKey) {
		project.create(deployrUrl, projectKey);
	}

	@Override
	public void deleteUpdates(String projectKey) {
		project.deleteUpdates(deployrUrl, projectKey);
	}
}